<?php

    class Home extends MY_Controller{
        
        public function __construct() 
        {
            parent::__construct();

        } 
        
        function index(){

            $this->load->model('News_Model');
            $allNews = $this->News_Model->getNews();
            $this->data['allNews'] = $allNews->result();
            
            $this->data['main_content'] = 'home';
            $this->load->view('includes/template', $this->data);
//            $this->load->view('test');
        }
        
        function login()
        {		
//            echo $this->input->post('username');
//            echo $this->input->post('password');
            try{
                $this->load->model('Member_Model');
                $query = $this->Member_Model->validate();
                
                if($query->num_rows() == 1) // if the user's credentials validated...
                {
                    $row = $query->row();

                    $data = array(
                                            'username' => $row->USERNAME,
                                            'memberID' => $row->ID,
                                            'user_type' => $row->USER_TYPE,
                                            'is_logged_in' => true
                    );
                    $this->session->set_userdata($data);
                    redirect('Home');
                }
                else // incorrect username or password
                {
                    show_error('Username or Password incorrect', 500, 'Error');
                }
            }catch(Exception $ex){
                show_error($ex->getMessage(), 500, 'Error Database');
            }
        }
        
        function logout(){
            $this->session->sess_destroy();
            redirect('home');
        }
        
        function signup() {
            $this->data['main_content'] = 'signup_test';
            
            for($i=0;$i<=30;$i++):
                $days[$i+1] = $i + 1;             
            endfor;

            $months = array(1 => "Jan",
                            2 => "Feb",
                            3 => "Mar",
                            4 => "Apr",
                            5 => "May",
                            6 => "Jun",
                            7 => "Jul",
                            8 => "Aug",
                            9 => "Sep",
                            10 => "Oct",
                            11 => "Nov",
                            12 => "Dec");
            
            $time=time();
            for($i=0;$i<=100;$i++):
                $years[date('Y',$time) - $i] = date('Y',$time) - $i;
            endfor;
            
            $this->data['day'] = $days;
            $this->data['month'] = $months;
            $this->data['year'] = $years;
            
            $this->load->view('includes/template', $this->data);
        }

        function validate_signup(){

            $username = $this->input->post('signup_username');
            $password = $this->input->post('signup_password');
            $idcard = $this->input->post('signup_idcard');
            $firstname = $this->input->post('signup_firstname');
            $lastname = $this->input->post('signup_lastname');
            $address = $this->input->post('signup_address');
            $telephone = $this->input->post('signup_telephone');
            $emal = $this->input->post('signup_email');

        }
    }
?>
